package thread.内部api;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class CyclicBarrierDe {

    private final static ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(4);

    static CyclicBarrier cyclicBarrier = new CyclicBarrier(4);



    public static void Te22st(){

        List<Future<Integer>> list = new ArrayList();

        ReentrantLock reentrantLock = new ReentrantLock();
        Condition condition = reentrantLock.newCondition();

        Future<Integer> submit = EXECUTOR_SERVICE.submit(() -> {

            reentrantLock.lock();
            condition.await();
            try {
                System.out.println("submit");
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                reentrantLock.unlock();
            }
            return 1;
        });

        Future<Integer> submit2 = EXECUTOR_SERVICE.submit(() -> {

            reentrantLock.lock();
            condition.await();
            try {
                System.out.println("submit2");
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                reentrantLock.unlock();
            }
            return 2;
        });

        Future<Integer> submit3 = EXECUTOR_SERVICE.submit(() -> {

            reentrantLock.lock();
            condition.await();
            try {
                System.out.println("submit3");
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                reentrantLock.unlock();
            }
            return 3;
        });

        list.add(submit);
        list.add(submit2);
        list.add(submit3);

        for (Future<Integer> booleanFuture : list) {

            try {
                reentrantLock.lock();
                condition.notify();
                Integer aBoolean = booleanFuture.get(300, TimeUnit.MILLISECONDS);
                System.out.println(aBoolean);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }finally {
                reentrantLock.unlock();
            }
        }


    }

    public static void main(String[] args) {
        Te22st();


//        for (int i = 0; i < 20; i++) {
//            int finalI = i;
//            EXECUTOR_SERVICE.execute(() -> {
//
//
//                try {
//                    Thread.sleep(1000 * (1+finalI));
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                if (2 == finalI) {
//                    System.out.println("rest");
//                    cyclicBarrier.reset();
//                }
//
//                try {
//
//                    System.out.println("start" + finalI);
//                    cyclicBarrier.await();
//
//                    System.out.println("end" + finalI);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                } catch (BrokenBarrierException e) {
//                    e.printStackTrace();
//                }
//
//            });
//
//
//        }



    }
}
